﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DockSample
{
    public partial class NonFunctionAddDialog : Form
    {
        public NonFunctionAddDialog(ObjectSpecificationDialog dialog)
        {
            InitializeComponent();
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            specificationDialog = dialog;
            initAssociationTypeComBox();
            initAssociationTargetComBox();
            setOptionsNFRComboBox("quantifiable");
            //associationType.SelectedIndex = 0;
        }
        ObjectSpecificationDialog specificationDialog;

        public string NonFunctionType
        {
            get{return quantifiable.Checked ? "Quantifiable" : "UnQuantifiable";}
            set{if (value.Equals("Quantifiable"))
                    quantifiable.Checked = true;
                else
                unquantifiable.Checked = true; }
        }
        public string NonFunctionName
        {
            get { return optionsNFRComboBox.Text; }
            set { optionsNFRComboBox.Text = value; }
        }
        public string NonFunctionValue
        {
            get { return NFRValueTextBox.Text; }
            set { NFRValueTextBox.Text = value; }
        }
        public string Description
        {
            get{return nonDescription.Text;}
            set{nonDescription.Text = value;}
        }
        public string AssociationType
        {
            get { return associationType.Text; }
            set { associationType.Text = value; }
        }
        public string AssociationTarget
        {
            get { return associationTarget.Text; }
            set { associationTarget.Text = value; }
        }
        //
        //事件响应函数
        private void associationType_SelectedValueChanged(object sender, EventArgs e)
        {
            initAssociationTargetComBox();
        }
        //
        private void quantifiable_CheckedChanged(object sender, EventArgs e)
        {
            //此处还要添加判断父类NFR是否是可度量，  再分别现实他们的实现代码
            if (this.quantifiable.Checked && this.newNFR.Checked)
            {
                setOptionsNFRComboBox("quantifiable");
            }
            else if (this.unquantifiable.Checked && this.newNFR.Checked)
            {
                setOptionsNFRComboBox("unquantifiable");
            }
        }
        //
        private void associationTarget_SelectedValueChanged(object sender, EventArgs e)
        {
            if (this.newNFR.Checked)
            {
                setOptionsNFRComboBox(this.quantifiable.Checked ? "quantifiable" : "unquantifiable");
            }
            else
            {
                if (specificationDialog.ParentOrderObjectDiagram != null)
                {
                    setOptionsNFRComboBox();
                }
            }
        }
        //根据可度量和不可度量特性设置可供选择非功能需求类型
        private void setOptionsNFRComboBox(string NFRType)
        {
            if (NFRType.Equals("quantifiable"))
            {
                this.optionsNFRComboBox.Items.Clear();
                this.optionsNFRComboBox.Items.Add("Reliability");
                this.optionsNFRComboBox.Items.Add("Availability");
                this.optionsNFRComboBox.Items.Add("Performance");
                this.optionsNFRComboBox.Items.Add("Usability");
                this.optionsNFRComboBox.Items.Add("Stress");
                this.optionsNFRComboBox.Items.Add("Volume");
                this.optionsNFRComboBox.Items.Add("Efficiency");
                this.optionsNFRComboBox.Items.Add("Other");
                this.optionsNFRComboBox.SelectedIndex = 0;
                this.NFRValueTextBox.Text = "";
            }
            else if (NFRType.Equals("unquantifiable"))
            {
                this.optionsNFRComboBox.Items.Clear();
                this.optionsNFRComboBox.Items.Add("Security");
                this.optionsNFRComboBox.Items.Add("Configuration");
                this.optionsNFRComboBox.Items.Add("Recovery");
                this.optionsNFRComboBox.Items.Add("Portability");
                this.optionsNFRComboBox.Items.Add("Other");
                this.optionsNFRComboBox.SelectedIndex = 0;
                this.NFRValueTextBox.Text = "";
            }
            else
                MessageBox.Show("Internal Error!---setNFRTypeComBox()");
        }
        //
        private void newNFR_CheckedChanged(object sender, EventArgs e)
        {
            initNFROptions();
        }
        //
        private void inheritNFR_CheckedChanged(object sender, EventArgs e)
        {
            initNFROptions();
        }
        //
        private void decompNFR_CheckedChanged(object sender, EventArgs e)
        {
            initNFROptions();
        }
        //
        private void optionsNFRComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            setNFRvalueComBox();
        }
        //
        //内部函数
        private void initAssociationTypeComBox()
        {
            this.associationType.Items.Add("Object");
            this.associationType.Items.Add("Attribute");
            this.associationType.Items.Add("Method");
            this.associationType.SelectedIndex = 0;
        }
        //根据对象/属性/方法设置可供选择的与非功能需求有关的模块名字
        private void initAssociationTargetComBox()
        {
            this.associationTarget.Items.Clear();
            switch (this.associationType.SelectedItem.ToString())
            {
                case "Attribute":
                    if (this.specificationDialog.TemporaryAttributeList.Count != 0)
                    {
                        foreach (Attribute attr in this.specificationDialog.TemporaryAttributeList)
                        {
                            this.associationTarget.Items.Add(attr.AttributeName);
                        }
                        this.associationTarget.SelectedIndex = 0;
                        //this.NFRValue.Text = this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList[0].NonFunctionValue;
                    }
                    else
                    {
                        if (!this.newNFR.Checked)
                        {
                            this.optionsNFRComboBox.Text = "";
                            this.optionsNFRComboBox.Items.Clear();
                            this.NFRValueTextBox.Text = "";
                        }
                    }
                    break;

                case "Method":
                    if (this.specificationDialog.TemporaryMethodList.Count != 0)
                    {
                        foreach (Method method in this.specificationDialog.TemporaryMethodList)
                        {
                            this.associationTarget.Items.Add(method.MethodName);
                        }
                        this.associationTarget.SelectedIndex = 0;
                        //this.NFRValue.Text = this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList[0].NonFunctionValue;
                    }
                    else
                    {
                        if (!this.newNFR.Checked)
                        {
                            this.optionsNFRComboBox.Text = "";
                            this.optionsNFRComboBox.Items.Clear();
                            this.NFRValueTextBox.Text = "";
                        }
                    }
                    break;

                case "Object":
                    this.associationTarget.Items.Add(this.specificationDialog.ObjectName);
                    this.associationTarget.SelectedIndex = 0;
                    //this.NFRValue.Text = this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList[0].NonFunctionValue;
                    break;
                default:
                    MessageBox.Show("Internal Error!");
                    break;
            }
        }
        //
        private void initNFROptions()
        {
            this.NFRValueTextBox.Text = "";

            if (this.newNFR.Checked)
            {
                this.OptionsType.Text = "NewNFR:";
                setOptionsNFRComboBox(this.quantifiable.Checked ? "quantifiable" : "unquantifiable");
            }
            else if (this.InheritNFR.Checked)
            {
                this.OptionsType.Text = "ParentNFR:";
                setOptionsNFRComboBox();
            }
            else if (this.decompNFR.Checked)
            {
                this.OptionsType.Text = "ParentNFR:";
                setOptionsNFRComboBox();
            }
            else
            {
                MessageBox.Show("Internal Error ---initNFROptions()");
            }
        }
        //
        private void setOptionsNFRComboBox()
        {
            this.optionsNFRComboBox.Items.Clear();
            if (this.specificationDialog.ParentOrderObjectDiagram != null)
            {
                if (this.associationType.Text.Equals("Object"))
                {
                    initNFRComBoxwithObjectNFR();
                }
                else if (this.associationType.Text.Equals("Attribute"))
                {
                    initNFRComBoxwithAttrNFR();
                }
                else if (this.associationType.Text.Equals("Method"))
                {
                    initNFRComBoxwithMethodNFR();
                }
                else
                {
                    MessageBox.Show("Internal Error!");
                }
            }
        }
        //
        private void initNFRComBoxwithObjectNFR()
        {
            if (this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList.Count != 0)
            {
                foreach (NonFunctionRequirment NFR in this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList)
                {
                    this.optionsNFRComboBox.Items.Add(NFR.NonFunctionName);
                }
                this.optionsNFRComboBox.SelectedIndex = 0;
                if (!this.decompNFR.Checked)
                {
                    this.NFRValueTextBox.Text = this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList[0].NonFunctionValue;
                }
            }
        }
        //
        private void initNFRComBoxwithAttrNFR()
        {
            //三层循环，可否改进？？？？？？
            foreach (Attribute parentAttr in this.specificationDialog.ParentOrderObjectDiagram.AttributeList)
            {
                foreach (Attribute attr in this.specificationDialog.TemporaryAttributeList)
                {
                    if (attr.AttributeName.Equals(this.associationTarget.Text) && attr.parentAttr.AttributeName.Equals(parentAttr.AttributeName) && parentAttr.AttributeNFRList.Count != 0)
                    {
                        foreach (NonFunctionRequirment NFR in parentAttr.AttributeNFRList)
                        {
                            this.optionsNFRComboBox.Items.Add(NFR.NonFunctionName);
                        }
                        this.optionsNFRComboBox.SelectedIndex = 0;
                        if (this.specificationDialog.ParentOrderObjectDiagram.AttributeList[0].AttributeNFRList.Count != 0 && !this.decompNFR.Checked)
                        {
                            this.NFRValueTextBox.Text = this.specificationDialog.ParentOrderObjectDiagram.AttributeList[0].AttributeNFRList[0].NonFunctionValue;
                        }
                    }
                }
            }
        }
        //
        private void initNFRComBoxwithMethodNFR()
        {
            foreach (Method parentMethod in this.specificationDialog.ParentOrderObjectDiagram.MethodList)
            {
                foreach (Method method in this.specificationDialog.TemporaryMethodList)
                {
                    if (method.MethodName.Equals(this.associationTarget.Text) && method.parentMethod.MethodName.Equals(parentMethod.MethodName) && parentMethod.MethodNFRList.Count != 0)
                    {
                        foreach (NonFunctionRequirment NFR in parentMethod.MethodNFRList)
                        {
                            this.optionsNFRComboBox.Items.Add(NFR.NonFunctionName);
                        }
                        this.optionsNFRComboBox.SelectedIndex = 0;
                        if (this.specificationDialog.ParentOrderObjectDiagram.MethodList[0].MethodNFRList.Count != 0 && !this.decompNFR.Checked)
                        {
                            this.NFRValueTextBox.Text = this.specificationDialog.ParentOrderObjectDiagram.MethodList[0].MethodNFRList[0].NonFunctionValue;
                        }
                    }
                }
            }
        }
        //
        private void setNFRvalueComBox()
        {
            this.NFRValueTextBox.Text = "";

            if (this.specificationDialog.ParentOrderObjectDiagram != null && !this.decompNFR.Checked)
            {
                if (this.associationType.Text.Equals("Object"))
                {
                    initNFRValueTextBoxwithObjectNFRValue();
                }
                else if (this.associationType.Text.Equals("Attribute"))
                {
                    initNFRValueTextBoxwithAttrNFRValue();
                }
                else if (this.associationType.Text.Equals("Method"))
                {
                    initNFRValueTextBoxwithMethodNFRValue();
                }
                else
                {
                    MessageBox.Show("Internal Error!");
                }
            }
        }
        //
        private void initNFRValueTextBoxwithObjectNFRValue()
        {
            if (this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList.Count != 0)
            {
                foreach (NonFunctionRequirment NFR in this.specificationDialog.ParentOrderObjectDiagram.ObjectNFRList)
                {
                    if(this.optionsNFRComboBox.Text.Equals(NFR.NonFunctionName))
                    {
                        this.NFRValueTextBox.Text = NFR.NonFunctionValue;
                        break;
                    }
                }
            }
        }
        //
        private void initNFRValueTextBoxwithAttrNFRValue()
        {
            foreach (Attribute parentAttr in this.specificationDialog.ParentOrderObjectDiagram.AttributeList)
            {
                bool findNFRValue = false;
                if (this.associationTarget.Text.Equals(parentAttr.AttributeName))
                {
                    foreach (NonFunctionRequirment NFR in parentAttr.AttributeNFRList)
                    {
                        if (this.optionsNFRComboBox.Text.Equals(NFR.NonFunctionName))
                        {
                            this.NFRValueTextBox.Text = NFR.NonFunctionValue;
                            findNFRValue = true;
                            break;
                        }
                    }
                }
                if (findNFRValue)
                {
                    break;
                }
            }
        }
        //
        private void initNFRValueTextBoxwithMethodNFRValue()
        {
            foreach (Method parentMethod in this.specificationDialog.ParentOrderObjectDiagram.MethodList)
            {
                bool findNFRValue = false;
                if (this.associationTarget.Text.Equals(parentMethod.MethodName))
                {
                    foreach (NonFunctionRequirment NFR in parentMethod.MethodNFRList)
                    {
                        if (this.optionsNFRComboBox.Text.Equals(NFR.NonFunctionName))
                        {
                            this.NFRValueTextBox.Text = NFR.NonFunctionValue;
                            findNFRValue = true;
                            break;
                        }
                    }
                }
                if (findNFRValue)
                {
                    break;
                }
            }
        }
    }

}
